/*
 * Created on Feb 12, 2008
 */
package maxygen;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.cycling74.max.*;

/**
 * A wrapper class for MaxPatcher.
 * 
 * @see     com.cycling74.max.MaxPatcher
 * @author  martinrobinson
 */
class PatcherWrapper {
    
    public enum SubType { NONE, ALL, MORE, NOTMORE }
    
    private MaxPatcher p;
    //private engine e;
    
    PatcherWrapper(MaxPatcher p)
    {
        this.p = p;
    }
    
    PatcherWrapper(MaxObject o)
    {
        this.p = o.getParentPatcher();
    }
    
    PatcherWrapper(BoxWrapper b)
    {
        this.p = b.getPatcher();
    }
    
    
    public MaxPatcher getPatcher()
    {
        return p;
    }
    
    public String getPath()
    {
        return p.getPath();
    }
    
    public String getName()
    {
        return p.getName();
    }
    
    public MaxWindow getWindow()
    {
        return p.getWindow();
    }
    
    public BoxWrapper[] getAllBoxes()
    {
        return BoxWrapper.wrapBoxes(p.getAllBoxes());
    }
    
    public BoxWrapper[] getAllBoxes(SubType mode)
    {
        if(mode == SubType.NONE) 
            return getAllBoxes();
        
        PatcherWrapper patchers[] = getSubPatchers(mode);
        BoxWrapper boxes[] = getAllBoxes();
        int count = boxes.length;
        
        int i, j;
        for(i = 0; i < patchers.length; i++) {
            BoxWrapper subBoxes[] = patchers[i].getAllBoxes();
            count += subBoxes.length;
        }
        
        BoxWrapper allBoxes[] = new BoxWrapper[count];
        
        int k = 0;
        for(j = 0; j < boxes.length; j++) {
            allBoxes[k++] = boxes[j];
        }
        
        for(i = 0; i < patchers.length; i++) {
            BoxWrapper subBoxes[] = patchers[i].getAllBoxes();
            for(j = 0; j < subBoxes.length; j++) {
                allBoxes[k++] = subBoxes[j];
            }
        }
        
        return allBoxes;
        
    }
    
    public PatcherWrapper[] getSubPatchers(SubType mode)
    {
        BoxWrapper boxes[] = getAllBoxes();
        int numPatchers = 0;
        
        int i;
        for(i = 0; i < boxes.length; i++) {
            if(boxes[i].isBoxPatcher(mode))
                numPatchers++;
        }
        
        PatcherWrapper patchers[] = new PatcherWrapper[numPatchers];
        
        int j = 0;
        for(i = 0; i < boxes.length; i++) {
            if(boxes[i].isBoxPatcher(mode))
                patchers[j++] = boxes[i].getSubPatcher();
        }
        
        return patchers;
    }
    
    /*// must rewrite this with BoxWrappers if it is re-inlcuded
    private MaxBox[] getSubPatchBoxes(int mode)
    {
        MaxBox boxes[] = getParentPatcher().getAllBoxes();
        int numPatchers = 0;
        
        int i;
        for(i = 0; i < boxes.length; i++) {
            if(isBoxPatcher(boxes[i]))
                numPatchers++;
        }
        
        MaxBox subPatchBoxes[] = new MaxBox[numPatchers];
        
        int j = 0;
        for(i = 0; i < boxes.length; i++) {
            if(isBoxPatcher(boxes[i]))
                subPatchBoxes[j++] = boxes[i];
        }
        
        return subPatchBoxes;
    }
    */
    
    
    public static PatcherWrapper[] wrapPatchers(MaxPatcher patchers[])
    {
        PatcherWrapper wrappedPatchers[] = new PatcherWrapper[patchers.length];
        for(int i = 0; i < patchers.length; i++) {
            wrappedPatchers[i] = new PatcherWrapper(patchers[i]);
        }
        
        return wrappedPatchers;
    }
}
